package com.cts.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.annotation.OperationLogger;
import com.cts.entity.User;
import com.cts.model.vo.PageResultVO;
import com.cts.model.vo.Result;
import com.cts.service.UserService;
import com.cts.util.JwtUtils;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @projectName: CBlog
 * @package: com.cts.controller.admin
 * @className: UserAdminController
 * @author: chentongsheng
 * @description: 用户管理
 * @date: 2023/2/6 20:58
 * @version: 1.0
 */

@RestController
@RequestMapping("/user")
@Api("用户管理")
public class UserAdminController {

    @Autowired
    UserService userService;

    @ApiOperation("用户登陆")
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        try {
            return userService.login(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("登陆失败");
    }

    @ApiOperation("获取用户信息")
    @GetMapping("")
    /**
     * 拦截token，利用token从redis中获取用户信息并封装在httpServletRequest中
     * @param HttpServletRequest httpServletRequest
     * @return
     */
    public Result getUser(HttpServletRequest httpServletRequest){
        try {
            return userService.getUserByUsername(httpServletRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("登陆失败");
    }

    @ApiOperation("注销登陆")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest httpServletRequest){
        try {
            return userService.logout(httpServletRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("请求失败");
    }

    @ApiOperation("刷新token")
    @GetMapping("/refreshToken")
    public Result refreshToken(HttpServletRequest httpServletRequest) {
        // 获取token
        String authorization = httpServletRequest.getHeader("Authorization");
        // 解析token
        Claims tokenBody = JwtUtils.getTokenBody(authorization);
        String userId = tokenBody.getSubject();
        String jwt = JwtUtils.generateToken(userId);
        HashMap<String, String> token = new HashMap<>();
        token.put("token", jwt);
        return Result.ok("请求成功", token);
    }

    @ApiOperation("更新用户")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", user.getId());
        userUpdateWrapper.set("username", user.getUsername());
        userUpdateWrapper.set("nickname", user.getNickname());
        userUpdateWrapper.set("email", user.getEmail());
        userUpdateWrapper.set("avatar", user.getAvatar());
        userService.update(userUpdateWrapper);
        return Result.ok("请求成功");
    }

    @ApiOperation("更新用户密码")
    @PostMapping("updateUserPassword")
    public Result updateUserPassword(@RequestParam String username, @RequestParam String password) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("username", username);
        userUpdateWrapper.set("password", encode);
        userService.update(userUpdateWrapper);
        return Result.ok("更新成功");
    }

}
